import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';
import nodejs from "@feidao/web/atom/browser/msg/nodejs";
import { uuid } from '@feidao/web/atom/core/str/uuid';
import upload from '../atom/upload';

export default async function (fd: IFeidaoAiBrowserComponent, ...args: any[]) {
	console.log('保存');
	const form_upd_staff = fd.data.node.querySelector<HTMLFormElement>('[data-feidao-id="form_upd_staff"]');
	const sex = form_upd_staff.sex.value;
	const staff_type = form_upd_staff.staff_type.value;
	const id_no = form_upd_staff.id_no.value;
	if (!id_no) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请录入身份证号');
		if (/\d{17}[\d|x]|\d{15}/.test(id_no) && (id_no.length == 15 || id_no.length == 18)) {
		} else {
			fire_component_msg('zj-common-002', 'ec-001', 'fail', '请录入正确格式的身份证号');
		}
		return;
	}
	const birthday = new Date(form_upd_staff.birthday.value).getTime();
	console.log(birthday);
	if (!birthday) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择出生日期');
		return;
	}
	const age = Number(form_upd_staff.age.value)
	if (!age) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请录入年龄');
		return;
	}
	const reg_per_res = form_upd_staff.reg_per_res.value;
	if (!reg_per_res) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请录入户口所在地');
		return;
	}
	const education = form_upd_staff.education.value;
	const bachelo_ocience = form_upd_staff.bachelo_ocience.value;
	const province = form_upd_staff.province.value;
	const native_place = form_upd_staff.native_place.value;
	const zodiac = form_upd_staff.zodiac.value;
	const blood_type = form_upd_staff.blood_type.value;
	const f_name = form_upd_staff.f_name.value;
	const marital_status = form_upd_staff.marital_status.value;
	const politics_face = form_upd_staff.politics_face.value;
	const entry_party_time = form_upd_staff.entry_party_time.value;
	let staff_picture = '';
	let accessory_url = '';
	//１、有则先上传文件 todo 同时删除之前上传到文件服务器的文件
	if (form_upd_staff.staff_picture.value) {
		const res_upload: any = await upload(fd, uuid(), form_upd_staff.staff_picture);
		console.log('pic------->', res_upload);
		if (res_upload == 'fail') {
			fire_component_msg('zj-common-002', 'ec-001', 'fail', '照片上传失败');
			return;
		} else if (res_upload.filename) {
			staff_picture = res_upload.filename;
			console.log('staff_picture--->', staff_picture);
		} else {
			fire_component_msg('zj-common-002', 'ec-001', 'fail', '照片上传失败');
			return;
		}
	}
	if (form_upd_staff.accessory_url.value) {
		const res_upload: any = await upload(fd, uuid(), form_upd_staff.accessory_url);
		console.log('2------->', res_upload);
		if (res_upload == 'fail') {
			fire_component_msg('zj-common-002', 'ec-001', 'fail', '附件上传失败');
			return;
		} else if (res_upload.filename) {
			accessory_url = res_upload.filename;
			console.log('accessory_url--->', accessory_url);
		} else {
			fire_component_msg('zj-common-002', 'ec-001', 'fail', '附件上传失败');
			return;
		}
	}
	const deptname = () => {
		const dept_sel = form_upd_staff.deptid;
		return dept_sel.options[dept_sel.options.selectedIndex].innerText;
	}
	console.log('deptname--->', deptname);
	//２、再保存数据
	const res = await nodejs(fd.data.spaceid, 'teach_staff_upd', {
		deptid: fd.data.deptid,
		deptname,
		sex, staff_type, id_no, birthday, age, reg_per_res, education, bachelo_ocience,
		province, native_place, zodiac, blood_type, f_name, marital_status, politics_face, entry_party_time, staff_picture, accessory_url
	});
	if (res === 'success') {
		fire_component_msg('zj-common-002', 'ec-001', 'success', '操作成功');
		fd.data.node.classList.add('display-n');
		fire_component_msg('zj-personnel-file-002', 'ec013');//刷新组件
	} else {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '操作失败');
	}
}
